#include "ParaView.h"
#include "ui_ParaView.h"

ParaView::ParaView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ParaView)
{
    ui->setupUi(this);
    this->setWindowTitle("参数预览");
    _Qdb = QSqlDatabase::addDatabase("QSQLITE","PARA");
    mode = new QSqlQueryModel;

    //m_model = new QStandardItemModel(1,12,this);

    radioGroup = new QButtonGroup;
    radioGroup->addButton(ui->rb1);
    radioGroup->addButton(ui->rb2);
    ui->tableView->setSortingEnabled(true);

    /*
        ** #1:排序模式
    */
    // ui->tableView->setModel(mode);
    radioGroup = new QButtonGroup;
    radioGroup->addButton(ui->rb1);
    radioGroup->addButton(ui->rb2);

    ui->tableView->setSortingEnabled(true);


    QSortFilterProxyModel *sqlproxy = new QSortFilterProxyModel(this);
    sqlproxy->setSourceModel(mode);
    ui->tableView->setModel(sqlproxy);


    /*
        ** #2:非排序模式
        ** ui->tableView->setModel(mode);
    */



    ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);
//    connect(radioGroup, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled),
//          [=](QAbstractButton *button, bool checked){
//            if(checked)
//    });
}

ParaView::~ParaView()
{
    _Qdb.close();
    delete ui;
}

void ParaView::on_tb_import_clicked()
{
    _Qdb.removeDatabase(ui->le_dbName->text());
    QString fileName = QFileDialog::getOpenFileName(this, tr("打开文件"),"db",
                                                      tr("SQL file (*.db)"));
    ui->le_dbName->setText(fileName);


    _Qdb.setDatabaseName(fileName.toStdString().data());
    _Qdb.open() ? undo() :pushWarn("打开参数数据库失败!",MU_ERROR_MODULE_SQL);

    QString queryStr = "select * from cad";
    mode->setQuery(queryStr,_Qdb);






//    mode->setHeaderData(0,Qt::Horizontal,QObject::tr("编号"));
//    mode->setHeaderData(1,Qt::Horizontal,QObject::tr("编号"));
//    mode->setHeaderData(2,Qt::Horizontal,QObject::tr("编号"));
//    mode->setHeaderData(3,Qt::Horizontal,QObject::tr("编号"));


}

void ParaView::on_cb_TB_activated(const QString &arg1)
{
    if(!arg1.isEmpty()){
        QString queryStr = "select * from " + arg1;
        mode->setQuery(queryStr,_Qdb);
        ui->rb1->setChecked(false);
        ui->rb2->setChecked(true);
    }
}

void ParaView::on_pB_set_clicked()
{
    if(ui->rb1->isChecked()){
        QString queryStr = ui->lineEdit->text();
        mode->setQuery(queryStr,_Qdb);
    }else {
        pushWarn("请打开手动sql脚本开关!",MU_ERROR_MODULE_OHTERWISE);
    }
}

void ParaView::on_rb1_toggled(bool checked)
{
    ui->pB_set->setEnabled(checked);
}

void ParaView::on_tableView_doubleClicked(const QModelIndex &index)
{
    // ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);

    // pushWarn("我正在测试!",MU_ERROR_MODULE_OHTERWISE);
    // ui->tableView->setFocus();
    ui->clip_borad->setText(mode->data(index).toString());

    pushWarn("我正在测试!",MU_ERROR_MODULE_OHTERWISE);
    // ui->tableView->setFocus();

}
